﻿
Imports LibMultiPlug.Core.Reporting
Imports DbDataLib

Public Class ReportEditForm
    Inherits WeifenLuo.WinFormsUI.Docking.DockContent
    Implements IReportable


    Private _report As Report
    Private _reportSerivce As LibMultiPlug.Core.Services.ServiceDefinitions.ReportService
    Private _errors As ErrorProvider

    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

        _reportSerivce = LibMultiPlug.Core.Services.ServiceManager.GetInstance().GetReportingService()
        _errors = New ErrorProvider
    End Sub


    Public Overloads Sub Show(ByVal dockPanel As WeifenLuo.WinFormsUI.Docking.DockPanel, ByVal repoId As Integer) Implements IReportable.Show

        _report = _reportSerivce.GetReport(repoId)

        Call InternalInitialize()

        MyBase.Show(dockPanel)

    End Sub

    Public Overloads Sub Show(ByVal dockPanel As WeifenLuo.WinFormsUI.Docking.DockPanel)

        _report = New Report

        Call InternalInitialize()

        MyBase.Show(dockPanel)
    End Sub

    Private Sub InternalInitialize()


        tbName.DataBindings.Clear()
        tbIdColumn.DataBindings.Clear()
        cbType.DataBindings.Clear()
        cbService.DataBindings.Clear()
        lblHeader.DataBindings.Clear()

        cbType.Items.Clear()
        For Each a As System.Reflection.Assembly In AppDomain.CurrentDomain.GetAssemblies()
            For Each t As Type In a.GetTypes()
                If t.GetInterface(GetType(IReportable).ToString()) Is GetType(IReportable) Then
                    cbType.Items.Add(t.ToString())
                End If
            Next
        Next

        cbService.Items.Clear()
        For Each key As String In DbDataRepository.GetInstance().Keys
            cbService.Items.Add(key)
        Next

        lblHeader.Text = String.Format("{0} (Id: {1})", IIf(tbName.Text = String.Empty OrElse tbName.Text Is Nothing, "kein Name", tbName.Text), _report.Id.ToString("0000000"))
        tbName.DataBindings.Add("Text", _report, "Name")
        tbIdColumn.DataBindings.Add("Text", _report, "IdColumn")
        cbService.DataBindings.Add("SelectedItem", _report, "ConnectionName")
        cbType.DataBindings.Add("SelectedItem", _report, "StartType")
        tbStatement.Text = _report.Statement

        dgvPreview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        Try
            dgvPreview.DataSource = Nothing
            dgvPreview.DataSource = _report.GetData()

            Try
                dgvPreview.Columns(_report.IdColumn).DefaultCellStyle.BackColor = Color.LightGreen
            Catch ex As Exception
                LibMultiPlug.Core.Statusmeldungen.Statusmeldungscontroller.SetLabelText(ex.Message, Color.Red)
            End Try

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub tbName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbName.TextChanged
        If tbName.Text.Length > 0 Then
            Me.Text = String.Format("Report definieren - {0}", tbName.Text)
        Else
            Me.Text = "Report definieren"
        End If
        lblHeader.Text = String.Format("{0} (Id: {1})", tbName.Text, _report.Id.ToString("0000000"))
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            _report.Validate()
            _reportSerivce.SaveReport(_report)
            Call InternalInitialize()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub tbStatement_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbStatement.Leave
        _report.Statement = tbStatement.Text
    End Sub

End Class